# -*- coding: utf-8 -*-
# @Time: 2021/8/13 15:20
# @File: problem-876.py
# @DESC: https://leetcode-cn.com/problems/linked-list-cycle/
# 环形链表，快慢指针

from list_node_common import ListNode, listToNode


class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        cur = head
        visited = set()
        while cur:
            if cur in visited:
                return True
            else:
                visited.add(cur)
            cur = cur.next
        return False

    # 快慢指针
    def hasCycleV2(self, head: ListNode) -> bool:

        if head == None or head.next == None:
            return False

        slow = head
        fast = head.next
        while slow != fast:
            if fast == None or fast.next == None:
                return False
            slow = slow.next
            fast = fast.next.next
        return True


data = [1, 2, 3, 4, 5]
l = listToNode(data)
so = Solution()
result = so.hasCycle(l)
print()
